/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.Box;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JToolBar;
import javax.swing.UIManager;
import javax.swing.border.LineBorder;

import org.jxSwing.component.JXComponent;
import org.jxSwing.i18n.I18N;
import org.jxUtils.check.CheckUtil;
import org.jxUtils.image.ImageUtil;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.panel
 * @date 08/02/2014 - 14:20:02
 */
public class ImageCustomPanel extends JPanel implements JXComponent {
	
	private static final long serialVersionUID = 7695217548804380522L;
	
	private boolean atributte = true;
	
	private boolean required = false;
	
	private boolean requiredBorderEnabled = true;
	
	private int index = -1;
	
	private ImageScrollPanel imageScrollPanel;
	
	private JToolBar pnlStatus;
	
	private JLabel lblPoint;
	
	private JLabel lblDimension;
	
	private Component hgStatus;
	
	private JProgressBar progressBar;
	
	private boolean showStatusPanel = true;
	
	/**
	 * 
	 */
	public ImageCustomPanel() {
	
		this(true);
	}
	
	/**
	 * @param showStatusPanel
	 */
	public ImageCustomPanel(final boolean showStatusPanel) {
	
		this(new ImageIcon(ImageUtil.newImage(1, 1)));
	}
	
	/**
	 * @param image
	 */
	public ImageCustomPanel(final ImageIcon image) {
	
		this(image, true);
	}
	
	/**
	 * @param image
	 * @param showStatusPanel
	 */
	public ImageCustomPanel(final ImageIcon image, final boolean showStatusPanel) {
	
		this.showStatusPanel = showStatusPanel;
		
		getImageScrollPanel().setImage(image);
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		setLayout(new BorderLayout());
		
		add(getImageScrollPanel(), BorderLayout.CENTER);
		add(getPnlStatus(), BorderLayout.SOUTH);
		
		final Dimension dimension = getImagePanel().getPreferredSize();
		
		//
		getLblDimension().setText("[" + (int)dimension.getWidth() + ", " + (int)dimension.getHeight() + "]");
		
		//
		getImagePanel().addMouseMotionListener(new MouseMotionAdapter() {
			
			@Override
			public void mouseMoved(final MouseEvent mouseEvent) {
			
				getLblPoint().setText("[" + mouseEvent.getX() + ", " + mouseEvent.getY() + "]");
			}
		});
	}
	
	/**
	 * @return imageIcon in byte array or null
	 */
	public final byte[] getByteArray() {
	
		return getImagePanel().getByteArray();
	}
	
	/**
	 * @return the hgStatus
	 */
	public Component getHgStatus() {
	
		if(hgStatus == null) {
			hgStatus = Box.createHorizontalGlue();
		}
		return hgStatus;
	}
	
	/**
	 * @return the image
	 */
	public ImageIcon getImage() {
	
		return getImagePanel().getImage();
	}
	
	/**
	 * @return the imagePanel
	 */
	public ImagePanel getImagePanel() {
	
		return getImageScrollPanel().getImagePanel();
	}
	
	/**
	 * @return the imageScrollPanel
	 */
	public ImageScrollPanel getImageScrollPanel() {
	
		if(imageScrollPanel == null) {
			imageScrollPanel = new ImageScrollPanel();
		}
		return imageScrollPanel;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#getIndex()
	 */
	@Override
	public int getIndex() {
	
		return index;
	}
	
	/**
	 * @return the lblDimension
	 */
	public JLabel getLblDimension() {
	
		if(lblDimension == null) {
			lblDimension = new JLabel("[0, 0]");
			lblDimension.setToolTipText("[Width, Height]");
		}
		return lblDimension;
	}
	
	/**
	 * @return the lblPoint
	 */
	public JLabel getLblPoint() {
	
		if(lblPoint == null) {
			lblPoint = new JLabel("[0, 0]");
			lblPoint.setToolTipText("[X, Y]");
		}
		return lblPoint;
	}
	
	/**
	 * @return the pnlStatus
	 */
	public JToolBar getPnlStatus() {
	
		if(pnlStatus == null) {
			pnlStatus = new JToolBar();
			pnlStatus.setFloatable(false);
			pnlStatus.setMargin(new Insets(5, 5, 0, 5));
			pnlStatus.setPreferredSize(new Dimension(30, 30));
			pnlStatus.add(getLblPoint());
			pnlStatus.add(getLblDimension());
			pnlStatus.add(getHgStatus());
			pnlStatus.add(getProgressBar());
			pnlStatus.setVisible(showStatusPanel);
		}
		return pnlStatus;
	}
	
	/**
	 * @return the progressBar
	 */
	public JProgressBar getProgressBar() {
	
		if(progressBar == null) {
			progressBar = new JProgressBar();
			progressBar.setMaximumSize(new Dimension(150, 15));
		}
		return progressBar;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isAtributte()
	 */
	@Override
	public boolean isAtributte() {
	
		return atributte;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequired()
	 */
	@Override
	public boolean isRequired() {
	
		return required;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#isRequiredBorderEnabled()
	 */
	@Override
	public boolean isRequiredBorderEnabled() {
	
		return requiredBorderEnabled;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setAtributte(boolean)
	 */
	@Override
	public void setAtributte(final boolean atributte) {
	
		this.atributte = atributte;
	}
	
	/**
	 * @param dataByte
	 */
	public final void setImage(final byte[] dataByte) {
	
		getImagePanel().setImage(dataByte);
	}
	
	/**
	 * @param dataByte
	 * @param adjustingScale
	 */
	public final void setImage(final byte[] dataByte, final boolean adjustingScale) {
		
		getImagePanel().setImage(dataByte, adjustingScale);
	}
	
	/**
	 * @param imageIcon
	 */
	public final void setImage(final ImageIcon imageIcon) {
		
		getImagePanel().setImage(imageIcon);
	}
	
	/**
	 * @param imageIcon
	 * @param adjustingScale
	 */
	public final void setImage(final ImageIcon imageIcon, final boolean adjustingScale) {
		
		getImagePanel().setImage(imageIcon, adjustingScale);
	}
	
	/**
	 * @param imageIcon
	 * @param newScale
	 */
	public final void setImage(final ImageIcon imageIcon, final double newScale) {
		
		getImagePanel().setImage(imageIcon, newScale);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setIndex(int)
	 */
	@Override
	public void setIndex(final int index) {
	
		this.index = index;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.awt.Component#setName(java.lang.String)
	 */
	@Override
	public void setName(final String name) {
	
		super.setName(name);
		
		setRequired(isRequired());
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequired(boolean)
	 */
	@Override
	public void setRequired(final boolean required) {
	
		this.required = required;
		
		//
		if(required) {
			
			//
			if(isRequiredBorderEnabled()) {
				
				setBorder(new LineBorder(Color.RED));
			}
			
			//
			if(CheckUtil.isNullOrAbsoluteEmpty(getName())) {
				
				setToolTipText(I18N.component().requiredThis());
				
			}else {
				
				setToolTipText(I18N.component().requiredField(getName()));
			}
			
		}else {
			
			setBorder(UIManager.getBorder("Panel.border"));
			setToolTipText(null);
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.jxSwing.component.JXComponent#setRequiredBorderEnabled(boolean)
	 */
	@Override
	public void setRequiredBorderEnabled(final boolean requiredBorderEnabled) {
	
		this.requiredBorderEnabled = requiredBorderEnabled;
	}
}
